package com.improvess.rna.som.output;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSlider;

import com.improvess.rna.som.KohonenSOM;

@SuppressWarnings("serial")
public class NewKohonenDialog extends JDialog {

	private JButton ok;
	private JButton cancel;
	private ShowNet showNet;
	private JComboBox comboSigmaZero;
	private JComboBox comboTalVizinhanca;
	private JComboBox comboTalAprendizagem;
	private JComboBox comboTaxaAprendizagem;
	private JComboBox comboNumeroNeuronios;
	private JSlider sliderAtributos;

	public NewKohonenDialog(ShowNet showNet) {
		super(showNet, true);
		this.showNet = showNet;

		init();
	}

	private void init() {
		Image img = new ImageIcon(this.getClass().getResource("edit24.png"))
				.getImage();
		this.setIconImage(img);

		JLabel title = new JLabel("Set your network:");
		title.setBounds(50, 10, 150, 30);
		this.getContentPane().add(title);

		this.setTitle("New kohonen SOM net");
		this.setBounds(350, 200, 300, 440);
		this.setResizable(false);
		this.setLayout(null);

		this.comboNumeroNeuronios = new JComboBox(new Integer[] { 1, 4, 9, 16,
				25, 36, 49, 64, 81 });
		this.comboNumeroNeuronios.setSelectedIndex(2);
		this.comboNumeroNeuronios.setBounds(150, 60, 60, 30);
		JLabel labelNumeroNeuronios = new JLabel("# Neurons:");
		labelNumeroNeuronios.setBounds(50, 60, 100, 30);
		this.getContentPane().add(this.comboNumeroNeuronios);
		this.getContentPane().add(labelNumeroNeuronios);

		this.comboSigmaZero = new JComboBox(new Double[] { 0.1, 0.2, 0.3, 0.4,
				0.5, 0.6, 0.7, 0.8, 0.9, 1.0 });
		this.comboSigmaZero.setSelectedIndex(1);
		this.comboSigmaZero.setBounds(150, 110, 60, 30);
		JLabel labelSigmaZero = new JLabel("Sigma Zero:");
		labelSigmaZero.setBounds(50, 110, 80, 30);
		this.getContentPane().add(this.comboSigmaZero);
		this.getContentPane().add(labelSigmaZero);

		this.comboTalVizinhanca = new JComboBox(new Integer[] { 500, 1000,
				1500, 2000 });
		this.comboTalVizinhanca.setSelectedIndex(1);
		this.comboTalVizinhanca.setBounds(150, 160, 60, 30);
		JLabel labelTalVizinhanca = new JLabel("Tal Sigma Zero:");
		labelTalVizinhanca.setBounds(50, 160, 100, 30);
		this.getContentPane().add(this.comboTalVizinhanca);
		this.getContentPane().add(labelTalVizinhanca);

		this.comboTalAprendizagem = new JComboBox(new Integer[] { 500, 1000,
				1500, 2000 });
		this.comboTalAprendizagem.setSelectedIndex(1);
		this.comboTalAprendizagem.setBounds(150, 210, 60, 30);
		JLabel labelTalAprendizagem = new JLabel("Tal Learning:");
		labelTalAprendizagem.setBounds(50, 210, 80, 30);
		this.getContentPane().add(this.comboTalAprendizagem);
		this.getContentPane().add(labelTalAprendizagem);

		this.comboTaxaAprendizagem = new JComboBox(new Double[] { 0.001, 0.01, 0.1, 0.2,
				0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 });
		this.comboTaxaAprendizagem.setSelectedIndex(0);
		this.comboTaxaAprendizagem.setBounds(150, 260, 80, 30);
		JLabel labelTaxaAprendizagem = new JLabel("Learning rate:");
		labelTaxaAprendizagem.setBounds(50, 260, 80, 30);
		this.getContentPane().add(this.comboTaxaAprendizagem);
		this.getContentPane().add(labelTaxaAprendizagem);

		this.sliderAtributos = new JSlider(JSlider.HORIZONTAL, 1, 10, 2);

		sliderAtributos.setMajorTickSpacing(1);
		sliderAtributos.setMinorTickSpacing(1);
		sliderAtributos.setPaintTicks(true);
		sliderAtributos.setPaintLabels(true);

		this.sliderAtributos.setBounds(150, 310, 120, 50);
		JLabel labelAtributos = new JLabel("# Attributes:");
		labelAtributos.setBounds(50, 310, 80, 30);
		this.getContentPane().add(this.sliderAtributos);
		this.getContentPane().add(labelAtributos);

		cancel = new JButton("Cancel");
		ok = new JButton("Ok!");
		ok.setBounds(100, 360, 80, 30);
		cancel.setBounds(200, 360, 80, 30);

		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				NewKohonenDialog.this.setVisible(false);
			}

		});

		ok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				double sigmaZero = (Double) NewKohonenDialog.this.comboSigmaZero
						.getSelectedItem();
				double talVizinhanca = (Integer) NewKohonenDialog.this.comboTalVizinhanca
						.getSelectedItem();
				double talAprendizagem = (Integer) NewKohonenDialog.this.comboTalAprendizagem
						.getSelectedItem();
				double taxaAprendizagemInicial = (Double) NewKohonenDialog.this.comboTaxaAprendizagem
						.getSelectedItem();

				int quantidadeAtributos = NewKohonenDialog.this.sliderAtributos.getValue();
				KohonenSOM redeSOM = new KohonenSOM(
						(Integer) NewKohonenDialog.this.comboNumeroNeuronios
								.getSelectedItem(), quantidadeAtributos,
						sigmaZero, talVizinhanca, talAprendizagem,
						taxaAprendizagemInicial, null);
				NewKohonenDialog.this.showNet.setRedeSOM(redeSOM);
				NewKohonenDialog.this.setVisible(false);
				NewKohonenDialog.this.showNet.repaint();
			}

		});

		this.getContentPane().add(ok);
		this.getContentPane().add(cancel);
	}

}
